package com.safeneeds.linguimana.entity.dao.impl;

import java.util.List;

import com.safeneeds.linguimana.dto.NoticeDataStorePort;
import com.safeneeds.linguimana.entity.PortGlutNotice;
import com.safeneeds.linguimana.entity.dao.IPortGlutNoticeDao;
import com.safeneeds.util.base.BaseHibernateDao;
import com.safeneeds.util.date.DateUtil;
import com.safeneeds.util.other.StringUtil;
import com.safeneeds.util.page.PageInfo;

public class PortGlutNoticeDao extends BaseHibernateDao<PortGlutNotice, String> implements IPortGlutNoticeDao{

	/**
	 * 获取前column条可以展示的通知公告
	 * 获取顺序为 置顶-> 更新日期
	 */
	@Override
	public List<PortGlutNotice> getNotice(int column) {
		String hsql="from PortGlutNotice where show = '1' order by stick desc,updateTime desc";
		return this.getEntitiesPage(hsql, 1, column);
	}

	//添加通知公告
	@Override
	public void addNotice(PortGlutNotice portGlutNotice) {
		// TODO Auto-generated method stub
		saveEntity(portGlutNotice);
	}

	/**
	 * 获取所有的通知公告的所有分类
	 * @param catelist
	 * @return 
	 */
	@Override
	public List<String> getNoticeAllCateDao(){
		// TODO Auto-generated method stub
		String sql="select distinct CATE from port_glut_notice";
		return this.getSQLQuery(sql);
		
	}

	/**
	 * 查询的数据 并分页
	 * @param pageInfo
	 * @return
	 */
	@Override
	public PageInfo listNoticePageDao(PageInfo pageInfo,
			NoticeDataStorePort noticedataStore) {
		// TODO Auto-generated method stub
		String hsql = "SELECT * FROM PORT_GLUT_NOTICE ";
		String countSql = "SELECT COUNT(*) FROM PORT_GLUT_NOTICE";
		
		String whereSql = "";
		String orderSql = " ORDER BY UPDATE_TIME desc";
		
		if(noticedataStore !=null){		
			whereSql += " where 1 = 1";
			//有分类
			if(!StringUtil.isEmpty(noticedataStore.getCate())){
				whereSql += " and CATE in ('" + noticedataStore.getCate() + "')";
			}
			
			//关键字
			if(!StringUtil.isEmpty(noticedataStore.getKeyWord())){
				whereSql += " and KEY_WORD like '%" + StringUtil.addQuote(noticedataStore.getKeyWord()) + "%'";
			}
			
			//状态是否发布隐藏草稿箱
			if(!StringUtil.isEmpty(noticedataStore.getShow())){
				whereSql += " and SHOW = '" + StringUtil.addQuote(noticedataStore.getShow()) + "'";
			}
			
			//时间
			if(!StringUtil.isEmpty(noticedataStore.getUpdateTimestart()) && !StringUtil.isEmpty(noticedataStore.getUpdateTimeend())){//有开始时间和结束时间
				if(DateUtil.compareDateYYYYMMDD(noticedataStore.getUpdateTimestart(), noticedataStore.getUpdateTimeend())){//开始时间大于结束时间
					whereSql += " and UPDATE_TIME >= to_timestamp('" + noticedataStore.getUpdateTimeend() + " 00:00:00', 'yyyy-MM-dd HH24:MI:SS')"
							 + " and UPDATE_TIME <= to_timestamp('" + noticedataStore.getUpdateTimestart() + " 23:59:59', 'yyyy-MM-dd HH24:MI:SS')";
				}else{
					whereSql += " and UPDATE_TIME <= to_timestamp('" + noticedataStore.getUpdateTimeend() + " 23:59:59', 'yyyy-MM-dd HH24:MI:SS')"
							 + " and UPDATE_TIME >= to_timestamp('" + noticedataStore.getUpdateTimestart() + " 00:00:00', 'yyyy-MM-dd HH24:MI:SS')";
				}
			}else if(!StringUtil.isEmpty(noticedataStore.getUpdateTimestart())){//只有开始时间
				whereSql += " and UPDATE_TIME >= to_timestamp('" + noticedataStore.getUpdateTimestart() + " 00:00:00', 'yyyy-MM-dd HH24:MI:SS')";
			}else if(!StringUtil.isEmpty(noticedataStore.getUpdateTimeend())){//只有结束时间
				whereSql += " and UPDATE_TIME <= to_timestamp('" + noticedataStore.getUpdateTimeend() + " 23:59:59', 'yyyy-MM-dd HH24:MI:SS')";
			}
		}
		pageInfo.setEntityClass(PortGlutNotice.class);
		pageInfo.setListHbSql(hsql+whereSql+orderSql);
		pageInfo.setTotalCountHbSQL(countSql+whereSql);
		return pageInfo;
	}

	@Override
	public PortGlutNotice getNoticeByIdDao(String noticeId) {
		// TODO Auto-generated method stub
		return this.findById(noticeId);
	}

	@Override
	public void updateNotice(PortGlutNotice noticeHis) {
		// TODO Auto-generated method stub
		this.updateEntity(noticeHis);
	}

	@Override
	public int updateNoticeShowDao(String ids, String isShow) {
		// TODO Auto-generated method stub
		String sql ="update PortGlutNotice set show = '"+isShow+"' where notice_id = '"+ids+"'";
		return this.currentSession().createQuery(sql).executeUpdate();
	}

	
	
}
